package com.lb.shopguide.manager.service.update;

import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AlertDialog;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;
import com.lb.shopguide.R;
import com.lb.shopguide.constants.AppConstant;
import com.lb.shopguide.entity.UpdateInfoBean;


/**
 * @author zk clunyes@126.com
 * @date 2015-5-21 上午10:35:03
 * @description 更新管理
 */

public class UpdateManager {
    private Context mContext;

    public UpdateManager(Context context) {
        this.mContext = context;

    }

    public void showUpdateInfo(final UpdateInfoBean updateBean, FragmentManager fragmentManager) {
        if (updateBean == null) {
            return;
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable(AppConstant.UPDATE_BEAN, updateBean);
        LbUpdateDialog dialog = new LbUpdateDialog(mContext, bundle);
        dialog.setOnButtonClickListener(mButtonClickListener);
        AlertDialog.Builder realDialog = dialog.getBuilder();
        final AlertDialog dialog1 = realDialog.create();
        dialog1.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface arg0) {
                dialog1.getButton(AlertDialog.BUTTON_POSITIVE).setTextColor(mContext.getResources().getColor(R.color.sg_common_red));
            }
        });
        dialog1.show();
    }

    private void openDownLoadService(String downUrl, String title) {
        final ICallbackResult callback = new ICallbackResult() {

            @Override
            public void OnBackResult(Object s) {
            }
        };
        ServiceConnection conn = new ServiceConnection() {

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }

            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                DownloadApkService.DownloadBinder binder = (DownloadApkService.DownloadBinder) service;
                binder.addCallback(callback);
                binder.start();
            }
        };
        Intent intent = new Intent(mContext, DownloadApkService.class);
        intent.putExtra(AppConstant.DOWNLOAD_URL, downUrl);
        intent.putExtra(AppConstant.DOWNLOAD_VERSION, title);
        mContext.startService(intent);
        mContext.bindService(intent, conn, Context.BIND_AUTO_CREATE);
    }


    private LbUpdateDialog.ButtonClickListener mButtonClickListener = new LbUpdateDialog.ButtonClickListener() {
        @Override
        public void onSureClick(LbUpdateDialog dialog, String url, String versionCode) {
            dialog.dismiss();
            openDownLoadService(url, versionCode);
        }

        @Override
        public void onCancelClick(LbUpdateDialog dialog, boolean forceDownload) {
            if (forceDownload) {
                ActivityUtils.getTopActivity().finish();
                AppUtils.exitApp();
            } else {
                dialog.dismiss();
            }
        }

        @Override
        public void onNoMoreClick(LbUpdateDialog dialog) {

        }
    };

}
